﻿'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'*******************************************************************

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging



Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Collections.ObjectModel


Imports System.Data
Imports System.IO
Imports System.Xml
Imports System.Collections

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Visio

Imports DC_Rack.Objects.clsDC_RackData


Public Class clsAttrDraw


#Region " Properties and declarations "

    Public m_db As Database
    Public m_ds As DataSet
    Public m_dt As DataTable

    Public m_dc As dc_Draw
    Public m_oc As Collection(Of dc_Draw)

    Private strUserId As String

    Public m_deleteCommand As DbCommand
    Public m_insertCommand As DbCommand
    Public m_updateCommand As DbCommand

    Public Const readAll As String = "selectTblAttrDraw"

    ' who am I
    Private myEnum As DC_RackTables = DC_RackTables.tblAttrDraw

#End Region

#Region " Move/Transform Data "


    '
    ' create columns
    '
    Public Sub CreateTableColumns(ByRef tempTable As DataTable)

        Dim newDC As dc_Draw = New dc_Draw(strUserId)

        Try

            Dim coldrawKey As New System.Data.DataColumn
            coldrawKey.ColumnName = "drawKey"
            coldrawKey.DataType = GetType(Int32)
            coldrawKey.DefaultValue = newDC.drawKey
            tempTable.Columns.Add(coldrawKey)

            Dim coldrawCompKey As New System.Data.DataColumn
            coldrawCompKey.ColumnName = "drawCompKey"
            coldrawCompKey.DataType = GetType(Int32)
            coldrawCompKey.DefaultValue = newDC.drawCompKey
            tempTable.Columns.Add(coldrawCompKey)

            Dim coldrawDocumentName As New System.Data.DataColumn
            coldrawDocumentName.ColumnName = "drawDocumentName"
            coldrawDocumentName.DataType = GetType(String)
            coldrawDocumentName.DefaultValue = newDC.drawDocumentName
            tempTable.Columns.Add(coldrawDocumentName)

            Dim coldrawPageName As New System.Data.DataColumn
            coldrawPageName.ColumnName = "drawPageName"
            coldrawPageName.DataType = GetType(String)
            coldrawPageName.DefaultValue = newDC.drawPageName
            tempTable.Columns.Add(coldrawPageName)

            Dim coldrawPinX As New System.Data.DataColumn
            coldrawPinX.ColumnName = "drawPinX"
            coldrawPinX.DataType = GetType(Double)
            coldrawPinX.DefaultValue = newDC.drawPinX
            tempTable.Columns.Add(coldrawPinX)

            Dim coldrawPinY As New System.Data.DataColumn
            coldrawPinY.ColumnName = "drawPinY"
            coldrawPinY.DataType = GetType(Double)
            coldrawPinY.DefaultValue = newDC.drawPinY
            tempTable.Columns.Add(coldrawPinY)

            Dim coldrawGeneric As New System.Data.DataColumn
            coldrawGeneric.ColumnName = "drawGeneric"
            coldrawGeneric.DataType = GetType(String)
            coldrawGeneric.DefaultValue = newDC.drawGeneric
            tempTable.Columns.Add(coldrawGeneric)

            Dim coldrawComments As New System.Data.DataColumn
            coldrawComments.ColumnName = "drawComments"
            coldrawComments.DataType = GetType(String)
            coldrawComments.DefaultValue = newDC.drawComments
            tempTable.Columns.Add(coldrawComments)

            Dim coldrawLastUpdate As New System.Data.DataColumn
            coldrawLastUpdate.ColumnName = "drawLastUpdate"
            coldrawLastUpdate.DataType = GetType(Date)
            coldrawLastUpdate.DefaultValue = newDC.drawLastEdit
            tempTable.Columns.Add(coldrawLastUpdate)

            Dim coldrawLastEdit As New System.Data.DataColumn
            coldrawLastEdit.ColumnName = "drawLastEdit"
            coldrawLastEdit.DataType = GetType(String)
            coldrawLastEdit.DefaultValue = newDC.drawLastEdit
            tempTable.Columns.Add(coldrawLastEdit)

            newDC = Nothing

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If newDC IsNot Nothing Then
            newDC = Nothing
        End If

    End Sub


    '
    ' use for writing records
    '
    Public Sub movePropertiesToDataRow _
            (ByRef tempRow As DataRow, _
             ByVal tempDC As dc_Draw)

        Try

            With tempDC
                tempRow.SetField("drawKey", .drawKey)
                tempRow.SetField("drawCompKey", .drawCompKey)
                tempRow.SetField("drawDocumentName", .drawDocumentName)
                tempRow.SetField("drawPageName", .drawPageName)
                tempRow.SetField("drawPinX", .drawPinX)
                tempRow.SetField("drawPinY", .drawPinY)
                tempRow.SetField("drawGeneric", .drawGeneric)
                tempRow.SetField("drawComments", .drawComments)
                tempRow.SetField("drawLastUpdate", .drawLastUpdate)
                tempRow.SetField("drawLastEdit", strUserId)
            End With



        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow)

        Try

            With m_dc
                .initProperties()
                .drawKey = tempRow.Item("drawKey")
                .drawCompKey = tempRow.Item("drawCompKey")
                .drawDocumentName = tempRow.Item("drawDocumentName")
                .drawPageName = tempRow.Item("drawPageName")
                .drawPinX = tempRow.Item("drawPinX")
                .drawPinY = tempRow.Item("drawPinY")
                .drawGeneric = tempRow.Item("drawGeneric")
                .drawComments = tempRow.Item("drawComments")
                .drawLastUpdate = tempRow.Item("drawLastUpdate")
                .drawLastEdit = tempRow.Item("drawLastEdit")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow, _
             ByRef tempDC As dc_Draw)

        Try

            With tempDC
                .initProperties()
                .drawKey = tempRow.Item("drawKey")
                .drawCompKey = tempRow.Item("drawCompKey")
                .drawDocumentName = tempRow.Item("drawDocumentName")
                .drawPageName = tempRow.Item("drawPageName")
                .drawPinX = tempRow.Item("drawPinX")
                .drawPinY = tempRow.Item("drawPinY")
                .drawGeneric = tempRow.Item("drawGeneric")
                .drawComments = tempRow.Item("drawComments")
                .drawLastUpdate = tempRow.Item("drawLastUpdate")
                .drawLastEdit = tempRow.Item("drawLastEdit")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' for moving active table to collection for wpf
    '
    Public Sub moveDataTableToObservableCollection _
                (ByVal dtTemp As DataTable)

        Try

            If 0 < dtTemp.Rows.Count Then

                If m_oc IsNot Nothing Then
                    m_oc = Nothing
                    m_oc = New Collection(Of dc_Draw)
                Else
                    m_oc = New Collection(Of dc_Draw)
                End If

                Dim drTemp As DataRow
                Dim dcTemp As dc_Draw
                For Each drTemp In dtTemp.Rows
                    dcTemp = New dc_Draw
                    moveDataRowToProperties(drTemp, dcTemp)
                    m_oc.Add(dcTemp)
                Next

            End If

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub


    '
    '
    '
    Public Function moveDataTableToXML() As XmlDocument

        Dim writer = New System.IO.StringWriter
        Dim m_dt_xml As Xml.XmlDocument = Nothing

        Try


            m_dt.WriteXml(writer, XmlWriteMode.WriteSchema, True)
            m_dt_xml = Nothing
            m_dt_xml = New Xml.XmlDocument
            m_dt_xml.LoadXml(writer.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            writer = Nothing

        End Try

        Return m_dt_xml


    End Function

#End Region

#Region " standard calls  "

    '
    '
    '
    Public Sub initDataSetAndTable()

        Try

            ' make sure the root stuff is there
            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema

            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_Draw)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_Draw)
            End If

            If m_dc Is Nothing Then
                m_dc = New dc_Draw(strUserId)
            Else
                m_dc = Nothing
                m_dc = New dc_Draw(strUserId)
            End If


            CreateTableColumns(m_dt)
            m_dt.TableName = dc_racktables.tblattrdraw.tostring
            m_ds.Tables.Add(m_dt)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub


    '
    '
    '
    Private Sub buildUpdateAttrDrawCommand()

        Try
            m_updateCommand = m_db.GetStoredProcCommand("updateAttrDraw")

            Dim cmdActive As DbCommand = m_updateCommand

            m_db.AddInParameter(cmdActive, "drawKey", DbType.Int32, "drawKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawCompKey", DbType.Int32, "drawCompKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawDocumentName", DbType.String, "drawDocumentName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawPageName", DbType.String, "drawPageName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawPinX", DbType.Double, "drawPinX", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawPinY", DbType.String, "drawPinY", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawGeneric", DbType.String, "drawGeneric", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawComments", DbType.String, "drawComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawLastUpdate", DbType.Date, "drawLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawLastEdit", DbType.String, "drawLastEdit", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildInsertAttrDrawCommand()

        Try
            m_insertCommand = m_db.GetStoredProcCommand("insertAttrDraw")

            Dim cmdActive As DbCommand = m_insertCommand

            m_db.AddInParameter(cmdActive, "drawKey", DbType.Int32, "drawKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawCompKey", DbType.Int32, "drawCompKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawDocumentName", DbType.String, "drawDocumentName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawPageName", DbType.String, "drawPageName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawPinX", DbType.Double, "drawPinX", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawPinY", DbType.String, "drawPinY", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawGeneric", DbType.String, "drawGeneric", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawComments", DbType.String, "drawComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawLastUpdate", DbType.Date, "drawLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "drawLastEdit", DbType.String, "drawLastEdit", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildDeleteAttrDrawCommand()

        Try
            m_deleteCommand = m_db.GetStoredProcCommand("deleteAttrDraw")

            Dim cmdActive As DbCommand = m_deleteCommand
            m_db.AddInParameter(cmdActive, "drawKey", DbType.Int32, "drawKey", DataRowVersion.Current)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' create a record based on property values move it to the generic datatable
    ' then copy the table to the database
    '
    Public Function addAttrDrawDataSourceRecord _
            (ByVal tempDC As dc_Draw, _
             Optional ByVal blnSilent As Boolean = False) _
             As Integer

        Dim intRowsAffected As Integer = 0

        Try

            initDataSetAndTable()

            Dim m_dr As DataRow = m_ds.Tables(dc_racktables.tblattrdraw.tostring).NewRow
            movePropertiesToDataRow(m_dr, tempDC)
            m_ds.Tables(dc_racktables.tblattrdraw.tostring).Rows.Add(m_dr)
            intRowsAffected = dbUpdateDataSet()

            If blnSilent = False Then
                MsgBox("DataRowsUpated = " & intRowsAffected)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return intRowsAffected

    End Function

    '
    '
    '
    Public Function dbUpdateDataSet() As Int16

        Dim rowsAffected As Int16 = 0

        Try

            rowsAffected = m_db.UpdateDataSet(m_ds, dc_racktables.tblattrdraw.tostring, _
                                        m_insertCommand, _
                                        m_updateCommand, _
                                        m_deleteCommand, _
                                        UpdateBehavior.Standard)
            'msgbox rowsAffected

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If

        End Try

        Return rowsAffected

    End Function




#End Region

#Region " Database Calls "

    '
    '
    '
    Public Sub dbSelectAllDrawData()


        Try


            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand(readAll)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblattrdraw.tostring)

            m_dt = m_ds.Tables(dc_racktables.tblattrdraw.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch Ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(Ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    '
    '
    Public Sub dbSelectAttrDrawByCompKey(ByVal intCompKey As Int32)


        Try

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectAttrDrawByCompKey")
            m_db.AddInParameter(cmd, "@compKey", DbType.Int32, intCompKey)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblattrdraw.tostring)

            m_dt = m_ds.Tables(dc_racktables.tblattrdraw.tostring)

            If m_dt.Rows.Count = 1 Then
                Dim m_dr As System.Data.DataRow = m_dt.Rows(0)
                moveDataRowToProperties(m_dr)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch Ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(Ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally
        End Try

    End Sub



#End Region

#Region " Constructor "

    Public Sub New()
        Try


            m_db = DatabaseFactory.CreateDatabase()

            strUserId = Environment.UserDomainName & "/" & Environment.UserName

            '
            initDataSetAndTable()

            m_updateCommand = Nothing
            m_insertCommand = Nothing
            m_deleteCommand = Nothing

            buildInsertAttrDrawCommand()
            buildDeleteAttrDrawCommand()
            buildUpdateAttrDrawCommand()



        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try




    End Sub


#End Region




End Class
